package plggnssclib

//#include "appmain.h"
//#include "skyex.h"
import "C"

import (
	"dgo/goutils/gnss"
	"dgo/goutils/utils"
	"fmt"
	"math"
	"time"
	"unsafe"
)

//	func maintest() {
//		C.maint()
//		Initialization()
//		//C.maint()
//		//C.SayHello(C.CString("Hello, World3\n"))
//		//demo3()
//		//Initialization()
//		err := errors.New("404")
//		var conn net.Conn
//		for {
//			// 调用*Reader结构体指针类型的读取方法
//			//
//			//var buf1 string
//			if err != nil {
//				conn, err = net.Dial("tcp", concfg.ip+":"+concfg.port)
//				defer conn.Close()
//				// 输出当前建Dial函数的返回值类型, 属于*net.TCPConn类型
//				buf1 := fmt.Sprintf("type:subscribe\r\nchannel:%s\r\n\r\n", concfg.channel_read)
//				_, err = conn.Write([]byte(buf1)) // 发送数据
//				if err != nil {
//					return
//				}
//			}
//			buf2 := [10240]byte{}
//			// 读取服务端发送的数据
//			_, err := conn.Read(buf2[:])
//			if err != nil {
//				fmt.Println("recv failed, err:", err)
//				return
//			}
//			//var sta=[3]float32{11111.000,22222.000,3000}
//			// 读取服务端发送的数据
//			/*if buf!=string(buf2[:]){
//				buf=string(buf2[:])
//				//DealEph(buf,len(buf))
//			}
//			*/
//			/*ci := C.int(n)
//			//var nKeynum C.int = n
//			pKey := (C.CString(string(buf[:n])))
//			C.Apipardata(&C.StaComDamo, pKey, ci)
//			result := C.replySat0()
//			str := C.GoString(result)
//			C.free(unsafe.Pointer(result))
//			fmt.Printf("msg2:%s\n", str)
//			if len(str) > 0 {
//				_, err = conn.Write([]byte(str)) // 发送数据
//				if err != nil {
//					return
//				}
//				fmt.Printf("send succed\n")
//			}*/
//			//fmt.Printf("msg2:%s\n",str1);
//			//fmt.Printf("msg2:%s\n",str1)
//			//s:=C.StaComDamo.comdata
//			//fmt.Printf("%s\n",s)
//			//C.splicingcommand(0,concfg.channel_read,"aaa",4)
//			//fmt.Println("客户端接收服务端发送的数据: ", string(buf[:n]))
//		}
//	}
func main() {
	str := "kplcors:123@119.96.169.117:12101/RTCM32EPH/$GNGGA,092726.000,2514.609440,N,11020.024110,E,2,17,1.05,150.335,M,0.00,M,2,1420*46"

	rval := gnss.ParseNtripClientConnectString(str)
	ntrip := gnss.NewNtripClient()
	ntrip.SetRemoteAddress(rval.Addr)
	ntrip.SetRequestMountPoint(rval.MountPoint)
	ntrip.SetNtripClientUser(rval.User, rval.Pass)

	C.Init()
	ntrip.SetOnRecvBuffer(func(sender *gnss.NtripClient, buffer []byte) {
		ci := C.int(len(buffer))
		c_buf := (*C.char)(unsafe.Pointer(&buffer[0]))
		C.skyex_inputbuf(C.obj, c_buf, ci)
		//C.free(unsafe.Pointer(buf)
	})
	utils.AddTaskAfterInterval("", time.Second*5, time.Second*5, func() bool {
		buf := []byte(rval.MakeNowNMEA() + "\r\n\r\n")
		ntrip.PostBuffer(buf)
		t := gnss.UTC2GPSTime(time.Now().UTC())
		week, tow := gnss.ToGPSTime(t)

		sod := math.Mod(tow, 86400)
		mjd := tow/86400 + float64(week)*7 + 44244

		Cmjd := C.int(mjd)
		Csod := C.double(sod)

		var stax = [3]float64{-1703955.3781, 4926094.3851, 3665912.5436}

		//fmt.Println(Cmjd,Csod)
		var outbuf [4096]byte
		c_char := (*C.char)(unsafe.Pointer(&outbuf[0]))
		c_stax := (*C.double)(unsafe.Pointer(&stax[0]))

		var p C.int
		p = C.skyex_calcu(C.obj, c_stax, c_char, Cmjd, Csod)
		fmt.Printf("outbuf:%s len:%d\n", outbuf, p)
		return true
	})
	ntrip.LogTypeStr = "TEST"
	ntrip.Open()
	for {
		time.Sleep(time.Second * 5)
	}
}
